package top.infopub.mgr.adm.service.impl;


import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.adm.dao.ServeRecordAttDao;
import top.infopub.mgr.adm.dao.ServeRecordDao;
import top.infopub.mgr.adm.domain.ServeRecordDomain;
import top.infopub.mgr.adm.service.ServeRecordService;
import top.infopub.mgr.adm.vo.ServeRecordVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.model.adm.ServeRecord;
import top.infopub.model.adm.ServeRecordAtt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class ServeRecordServiceImpl extends GenericServiceImpl<ServeRecord, String> implements ServeRecordService {

    @Autowired
    private ServeRecordDao serveRecordDao;

    @Autowired
    private ServeRecordAttDao serveRecordAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<ServeRecord, String> getDao() {
        return serveRecordDao;
    }

    @Override
    public Page<ServeRecordDomain> selectByDomainWhere(Page<ServeRecordDomain> page,
                                                       ServeRecordVo serveRecordVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ServeRecordDomain>)serveRecordDao.selectByDomainWhere(serveRecordVo);
    }

    @Override
    public Page<ServeRecordDomain> selectMyCheckByDomainWhere(Page<ServeRecordDomain> page,
                                                              ServeRecordVo serveRecordVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ServeRecordDomain>)serveRecordDao.selectMyCheckByDomainWhere(serveRecordVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveServeRecord(ServeRecord serveRecord, List<ServeRecordAtt> serveRecordAtts) {
        
        serveRecordDao.insertById(serveRecord);
        
        if(CollectionUtils.isNotEmpty(serveRecordAtts)){
            serveRecordAttDao.insertByBatch(serveRecordAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.ADM_SERVE_RECORD;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, serveRecord.getId(), serveRecord.getProcessId(), null);
    }

    @Override
    public ServeRecordVo queryById(String id) {
        return serveRecordDao.queryById(id);
    }

    @Override
    public List<ServeRecord> isExistTime(ServeRecord serveRecord) {
        return serveRecordDao.isExistTime(serveRecord);
    }
}